package com.cheerful.blog.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cheerful.blog.dao.CategoryDao;
import com.cheerful.blog.entity.Category;
import com.cheerful.blog.service.ArticleService;
import com.cheerful.blog.service.CategoryService;
import com.cheerful.blog.to.CategoryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * (Category)表服务实现类
 *
 * @author makejava
 * @since 2022-02-07 13:15:03
 */
@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, Category> implements CategoryService {
    @Autowired
    ArticleService articleService;

    @Override
    public List<CategoryVo> listWithCount() {
        List<Category> categories = this.list();
        List<CategoryVo> collect = categories.stream().map(category -> {
            CategoryVo categoryVo = new CategoryVo();
            Long id = category.getId();
            Integer count = articleService.countCategory(id);
            categoryVo.setCount(count);
            BeanUtils.copyProperties(category, categoryVo);
            return categoryVo;
        }).collect(Collectors.toList());
        return collect;
    }
}

